package com.kakarote.admin.controller;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kakarote.admin.entity.PO.AdminCompanySecuritySetting;
import com.kakarote.admin.entity.PO.AdminConfig;
import com.kakarote.admin.service.IAdminCompanySecuritySettingService;
import com.kakarote.admin.service.IAdminConfigService;
import com.kakarote.common.log.annotation.OperateLog;
import com.kakarote.common.log.entity.OperationLog;
import com.kakarote.common.log.entity.OperationResult;
import com.kakarote.common.log.enums.ApplyEnum;
import com.kakarote.common.log.enums.BehaviorEnum;
import com.kakarote.common.log.enums.OperateObjectEnum;
import com.kakarote.common.log.enums.OperateTypeEnum;
import com.kakarote.core.common.ParamAspect;
import com.kakarote.core.common.Result;
import com.kakarote.core.common.enums.SystemCodeEnum;
import com.kakarote.core.entity.AdminCompanySecuritySettingVO;
import com.kakarote.core.entity.BasePage;
import com.kakarote.core.entity.PageEntity;
import com.kakarote.core.exception.CrmException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 企业安全设置表 前端控制器
 * </p>
 *
 * @author yangzenghui
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/adminCompanySecuritySetting")
@Api(tags = "员工安全设置相关接口")
public class AdminCompanySecuritySettingController {

    @Autowired
    private IAdminCompanySecuritySettingService adminCompanySecuritySettingService;

    @Autowired
    private IAdminConfigService adminConfigService;

    @PostMapping("/queryById/{id}")
    @ApiOperation("根据ID查询")
    public Result<AdminCompanySecuritySetting> queryById(@PathVariable("id") @ApiParam(name = "id", value = "id") Serializable id) {
        AdminCompanySecuritySetting entity = adminCompanySecuritySettingService.queryById(id);
        return Result.ok(entity);
    }

    @PostMapping("/queryByConfigType/{configType}")
    @ApiOperation("根据设置类型查询企业设置")
    public Result<AdminCompanySecuritySettingVO> queryByConfigType(@PathVariable("configType") @ApiParam(name =
            "configType", value = "configType") Integer configType) {
        AdminCompanySecuritySettingVO entity = adminCompanySecuritySettingService.queryByConfigType(configType);
        return Result.ok(entity);
    }

    @PostMapping("/queryByCompanyIdType/{configType}")
    @ApiOperation("根据设置类型和企业id查询企业设置")
    public Result<JSONObject> queryByCompanyIdType(@PathVariable("configType") @ApiParam(name = "configType", value =
            "configType") Integer configType) {
        AdminCompanySecuritySettingVO entity =
                adminCompanySecuritySettingService.queryByConfigTypeAndCompanyId(configType);
        return Result.ok(JSONObject.parseObject(JSONObject.toJSONString(entity)));
    }

    @PostMapping("/add")
    @ApiOperation("保存数据")
    public Result<String> add(@RequestBody AdminCompanySecuritySettingVO vo) {
        adminCompanySecuritySettingService.add(vo);
        return Result.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改数据")
    @OperateLog(apply = ApplyEnum.SYSTEM_SETTING, object = OperateObjectEnum.COMPANY_SECURITY_SETTING, type = OperateTypeEnum.SETTING, behavior = BehaviorEnum.UPDATE)
    public Result<String> update(@RequestBody AdminCompanySecuritySettingVO vo) {
        adminCompanySecuritySettingService.update(vo);
        if (ObjectUtil.equal(2, vo.getConfigType())) {
            if (StrUtil.length(String.valueOf(vo.getMaximumIdleTime())) > 9 || StrUtil.length(String.valueOf(vo.getMaximumLoginFreeTime())) > 9) {
                throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
            }
        }
        OperationLog operationLog = new OperationLog();
        if (vo.getConfigType() == 1) {
            operationLog.setOperationObject("密码规则设置");
            operationLog.setOperationInfo("修改密码规则设置");
        } else if (vo.getConfigType() == 2) {

            operationLog.setOperationObject("登出规则设置");
            operationLog.setOperationInfo("修改登出规则设置");
        }
        return OperationResult.ok(operationLog);
    }

    @PostMapping("/queryPageList")
    @ApiOperation("查询列表页数据")
    public Result<BasePage<AdminCompanySecuritySetting>> queryPageList(@RequestBody PageEntity search) {
        search.setPageType(1);
        return Result.ok(adminCompanySecuritySettingService.queryPageList(search));
    }

    @PostMapping("/deleteByIds")
    @ApiOperation("根据ID删除数据")
    public Result<String> deleteByIds(@ApiParam(name = "ids", value = "id列表") @RequestBody List<Serializable> ids) {
        adminCompanySecuritySettingService.deleteByIds(ids);
        return Result.ok();
    }


    @PostMapping("/queryWaterMarkConfig")
    @ApiOperation("查看水印设置")
    public Result queryBlogConfig() {
        AdminConfig adminConfig = adminConfigService.queryConfigByName("WaterMarkConfig");
        if (ObjectUtil.isNull(adminConfig)) {
            //初始化
            adminConfig = new AdminConfig();
            adminConfig.setStatus(0);
            Dict dict = Dict.create().set("content", "").set("fontSize", "12px")
                    .set("density", "1").set("color", "#42526E").set("angle", "20");
            adminConfig.setValue(JSON.toJSONString(dict));
            adminConfig.setName("WaterMarkConfig");
            adminConfigService.save(adminConfig);
        }
        return Result.ok(adminConfig);
    }

    @PostMapping("/updateWaterMarkConfig")
    @ApiOperation("修改水印设置")
    @OperateLog(apply = ApplyEnum.SYSTEM_SETTING, object = OperateObjectEnum.COMPANY_SECURITY_SETTING, type = OperateTypeEnum.SETTING, behavior = BehaviorEnum.UPDATE)
    public Result updateBlogConfig(@RequestBody AdminConfig adminConfig) {
        adminConfigService.updateAdminConfig(adminConfig);

        OperationLog operationLog = new OperationLog();
        operationLog.setOperationObject("水印设置");
        operationLog.setOperationInfo("修改水印设置,状态为：" + (adminConfig.getStatus() == 1 ? "启用" : "不启用"));
        return OperationResult.ok(operationLog);
    }

    @PostMapping("/querySecuritySetting/{configType}/{companyId}")
    @ParamAspect
    @ApiOperation("查询企业安全设置(未登录时)")
    public Result<AdminCompanySecuritySettingVO> querySecuritySetting(@PathVariable("configType") @ApiParam(name = "configType", value =
            "configType") Integer configType, @PathVariable("companyId") @ApiParam(name = "companyId", value =
            "companyId") Long companyId) {
        return Result.ok(adminCompanySecuritySettingService.querySecuritySetting(configType, companyId));
    }
}
